package chapter3;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author caiya
 * @date 2020/3/24 22:09
 */
public class AtomicLongTest {
    static AtomicLong atomicLong = new AtomicLong();

    static long[] data1 = {0,1,2,3,4,6,0,3434,0,23,1,0};
    static long[] data2 = {0,1,0,3,4,6,0,0,0,23,1,0};

    public static void main(String[] args) throws InterruptedException {
        /**
         * AtomicLong 类似 AtomicInteger
         */
        // 统计data1、data2中0的个数总和
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                for (long data: data1) {
                    if (data == 0) {
                        atomicLong.incrementAndGet();
                    }
                }
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (long data: data2) {
                    if (data == 0) {
                        atomicLong.incrementAndGet();
                    }
                }
            }
        });

        thread.start();
        thread1.start();

        thread.join();
        thread1.join();

        System.out.println("总计: " + atomicLong.get());
    }
}
